<?php
/*
 * 车型库评论
 * @package    auto
 * @author     Sameal
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: 7ed814382bd1e5e6beb39e482a5561bdf10c74d8 $
 */
defined('IN_IXDCW') or exit('Access Denied');
/**
 * Class AutoCarParam
 */
class AutoCarComment extends BasicModule {

	/** @var string 表格 */
	private $_table;
	/** @var array 可用字段 */
	private $_fields;

	function __construct() {
		parent::__construct();
		$this->_table    = Table::name(Table::NAME_AUTO_COMMENT);
		$this->_pagesize = 5;
		$this->_fields   = array('id', 'arbid', 'armid', 'arcid', 'amid', 'userid', 'app', 'inners', 'oil', 'quality', 'config', 'space', 'comfort', 'detail', 'speed', 'oper', 'noise', 'userfeeling', 'addtime', 'ip');
	}

	/**
	 * 完善数据 增加ip addtime
	 * @param $post
	 * @return array
	 */
	function set($post) {
		global $DT_TIME, $DT_IP;
		$post['addtime'] = $DT_TIME;
		$post['ip']      = $DT_IP;
		return $post;
	}

	/**
	 * 检查数据
	 * @param $post 表单提交数据
	 * @oaram $timeLimte 时限
	 * @return fetch
	 */
	function check($post, $timeLimte) {
		global $DT_TIME;
		$sql = '';
		foreach ($post as $k => $v) {
			$sql .= " " . $k . "=" . "'$v' AND";
		}
		$sql  = "SELECT * FROM {$this->_table} WHERE $sql addtime > " . ($DT_TIME - $timeLimte);
		$item = $this->_db->getOne($sql);
		return $item;
	}

	/**
	 * 获取某车评论
	 * @param $modelId 车款
	 * @oaram $carId 车型
	 * @return fetch
	 */

	public function getList($modelId, $carId) {
		if ($modelId) {
			$sql = "SELECT * FROM {$this->_table} WHERE amid=$modelId";
		} else {
			$sql = "SELECT * FROM {$this->_table} WHERE arcid=$carId";
		}
		$sql .= " ORDER BY addtime DESC";
		$sql .= " LIMIT " . ($this->_pn->getPage() - 1) * $this->_pagesize . ',' . $this->_pagesize;
		$result = $this->_db->query($sql);
		while ($r = $this->_db->fetchArray($result)) {
			$r['username'] = get_user($r['userid'], 'userid', $from = 'truename');
			$r['date']     = date('Y-m-d', $r['addtime']);
			if ($modelId) {
				$r['carname'] = get_carModuleid($modelId);
				$r['linkurl'] = url('auto/model', 'modelid='.$modelId);
			} else {
				if ($r['amid']) {
					$r['carname'] = get_carModuleid($r['amid']);
					$r['linkurl'] = url('auto/model', 'modelid='.$r['amid']);
				} else {
					$r['carname'] = get_carName($carId);
					$r['linkurl'] = url('auto/car', 'carid='.$carId);
				}
			}
			$lists[] = $r;
		}
		return $lists;
	}

	/**
	 * 获取某车评论数量
	 * @param $modelId 车款
	 * @oaram $carId 车型
	 * @return int
	 */
	public function getCount($modelId, $carId) {
		if ($modelId) {
			$sql = "SELECT COUNT(*) FROM {$this->_table} WHERE amid=$modelId ";
		} else {
			$sql = "SELECT COUNT(*) FROM {$this->_table} WHERE arcid=$carId ";
		}
		$num   = $this->_db->getVar($sql);
		$items = $num;
		$this->_pn->recordsPerPage($this->_pagesize);
		$this->_pn->records($items);
		return $items;
	}


	/**
	 * 插入数据
	 * @param $post 表单提交数据
	 * @return string
	 */
	public function add($post) {
		//60秒之内 数据重复提交
		$query = $this->check($post, 60);
		if (isset($query)) {
			return 'ALL';
		}

		//完善数据 增加ip addtime
		$post = $this->set($post);

		//同一用户针对同一款车多次评论
		$checkpost = array();
		if ($post['amid']) {
			$checkpost['amid'] = $post['amid'];
		} else {
			$checkpost['arcid'] = $post['arcid'];
			$checkpost['amid']  = 0;
		}
		$checkpost['userid'] = $post['userid'];
		$query               = $this->check($checkpost, 1 * 24 * 60 * 60);
		if (isset($query)) {
			return 'USERID';
		}

		$checkpost = array();
		//同一ip针对同一款车多次评论

		if ($post['amid']) {
			$checkpost['amid'] = $post['amid'];
		} else {
			$checkpost['arcid'] = $post['arcid'];
			$checkpost['amid']  = 0;
		}
		$checkpost['ip'] = $post['ip'];
		$query           = $this->check($checkpost, 1 * 24 * 60 * 60);
		if (isset($query)) {
			return 'IP';
		}

		//成功返回
		if (!isset($query)) {
			$this->_db->query(Sql::insert($this->_table, $post, $this->_fields));
			return 1;
		}
	}

}